class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size + 1))  # 1-based indexing
        self.size = [1] * (size + 1)
    
    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]
    
    def union(self, x, y):
        x_root = self.find(x)
        y_root = self.find(y)
        if x_root == y_root:
            return
        # Merge smaller set into larger set
        if self.size[x_root] < self.size[y_root]:
            x_root, y_root = y_root, x_root
        self.parent[y_root] = x_root
        self.size[x_root] += self.size[y_root]

def main():
    n,m,L,R = map(int,input().split())
    
    edges = []
    for _ in range(m):
        u,v,w = map(int,input().split())
        edges.append( (w, u, v) )
    edges.sort()
    
    uf = UnionFind(n)
    ans = 0
    for w, u, v in edges:
        root_u = uf.find(u)
        root_v = uf.find(v)
        if root_u != root_v:
            if L <= w <= R:
                ans += uf.size[root_u] * uf.size[root_v]
            uf.union(u, v)
    print(ans)

if __name__ == "__main__":
    main()